#pragma once

#include <cppconn/connection.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/prepared_statement.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <iostream>
#include <memory>
#include <mysql/jdbc.h>
#include <string>
#include <vector>

using namespace std;
using namespace sql;

struct Author {
    int _id;
    std::string _name;
    std::string _brief_introduction;
    int _level;
    bool _is_transcription;
    int _message_id; // 注意：当_message_id为0时表示数据库NULL值
};

class AuthorMapper {
private:
    sql::mysql::MySQL_Driver* driver;
    std::unique_ptr<sql::Connection> conn;

public:
    AuthorMapper(const std::string& host, const std::string& user, 
                const std::string& password, const std::string& database)
    {
        driver = sql::mysql::get_mysql_driver_instance();
        conn.reset(driver->connect(host, user, password));
        conn->setSchema(database);
    }

    // 插入新作者
    int insertAuthor(const Author& author) {
        std::string sql = "INSERT INTO authors "
                         "(_name, _brief_introduction, _level, _is_transcription, _message_id) "
                         "VALUES (?, ?, ?, ?, ?)";
        
        std::unique_ptr<sql::PreparedStatement> pstmt(conn->prepareStatement(sql));
        pstmt->setString(1, author._name);
        pstmt->setString(2, author._brief_introduction);
        pstmt->setInt(3, author._level);
        pstmt->setBoolean(4, author._is_transcription);
        
        // 处理NULL值
        if (author._message_id != 0) {
            pstmt->setInt(5, author._message_id);
        } else {
            pstmt->setNull(5, sql::DataType::INTEGER);
        }
        
        pstmt->executeUpdate();
        
        // 获取自增ID
        pstmt = std::unique_ptr<sql::PreparedStatement>(conn->prepareStatement("SELECT LAST_INSERT_ID()"));
        std::unique_ptr<sql::ResultSet> res(pstmt->executeQuery());
        if (res->next()) {
            return res->getInt(1);
        }
        return -1; // 插入失败
    }

    // 删除作者
    bool deleteAuthor(int authorId) {
        std::string sql = "DELETE FROM authors WHERE _id = ?";
        std::unique_ptr<sql::PreparedStatement> pstmt(conn->prepareStatement(sql));
        pstmt->setInt(1, authorId);
        return pstmt->executeUpdate() > 0;
    }

    // 更新作者信息
    bool updateAuthor(const Author& author) {
        std::string sql = "UPDATE authors SET "
                         "_name = ?, "
                         "_brief_introduction = ?, "
                         "_level = ?, "
                         "_is_transcription = ?, "
                         "_message_id = ? "
                         "WHERE _id = ?";
        
        std::unique_ptr<sql::PreparedStatement> pstmt(conn->prepareStatement(sql));
        pstmt->setString(1, author._name);
        pstmt->setString(2, author._brief_introduction);
        pstmt->setInt(3, author._level);
        pstmt->setBoolean(4, author._is_transcription);
        
        // 处理NULL值
        if (author._message_id != 0) {
            pstmt->setInt(5, author._message_id);
        } else {
            pstmt->setNull(5, sql::DataType::INTEGER);
        }
        
        pstmt->setInt(6, author._id);
        return pstmt->executeUpdate() > 0;
    }

    // 根据ID获取作者信息
    Author getAuthorById(int authorId) {
        Author author;
        author._id = authorId;
        
        std::string sql = "SELECT _name, _brief_introduction, _level, "
                         "_is_transcription, _message_id "
                         "FROM authors WHERE _id = ?";
        
        std::unique_ptr<sql::PreparedStatement> pstmt(conn->prepareStatement(sql));
        pstmt->setInt(1, authorId);
        std::unique_ptr<sql::ResultSet> res(pstmt->executeQuery());
        
        if (res->next()) {
            author._name = res->getString("_name");
            author._brief_introduction = res->getString("_brief_introduction");
            author._level = res->getInt("_level");
            author._is_transcription = res->getBoolean("_is_transcription");
            author._message_id = res->isNull("_message_id") ? 0 : res->getInt("_message_id");
        } else {
            throw std::runtime_error("Author not found with ID: " + std::to_string(authorId));
        }
        
        return author;
    }

    // 按名称查找作者
    std::vector<Author> getAuthorsByName(const std::string& name) {
        std::vector<Author> authors;
        
        std::string sql = "SELECT _id, _name, _brief_introduction, _level, "
                         "_is_transcription, _message_id "
                         "FROM authors WHERE _name LIKE ?";
        
        std::unique_ptr<sql::PreparedStatement> pstmt(conn->prepareStatement(sql));
        pstmt->setString(1, "%" + name + "%");
        std::unique_ptr<sql::ResultSet> res(pstmt->executeQuery());
        
        while (res->next()) {
            Author author;
            author._id = res->getInt("_id");
            author._name = res->getString("_name");
            author._brief_introduction = res->getString("_brief_introduction");
            author._level = res->getInt("_level");
            author._is_transcription = res->getBoolean("_is_transcription");
            author._message_id = res->isNull("_message_id") ? 0 : res->getInt("_message_id");
            authors.push_back(author);
        }
        
        return authors;
    }

    // 获取所有作者列表
    std::vector<Author> getAllAuthors() {
        std::vector<Author> authors;
        
        std::string sql = "SELECT _id, _name, _brief_introduction, _level, "
                         "_is_transcription, _message_id FROM authors";
        
        std::unique_ptr<sql::Statement> stmt(conn->createStatement());
        std::unique_ptr<sql::ResultSet> res(stmt->executeQuery(sql));
        
        while (res->next()) {
            Author author;
            author._id = res->getInt("_id");
            author._name = res->getString("_name");
            author._brief_introduction = res->getString("_brief_introduction");
            author._level = res->getInt("_level");
            author._is_transcription = res->getBoolean("_is_transcription");
            author._message_id = res->isNull("_message_id") ? 0 : res->getInt("_message_id");
            authors.push_back(author);
        }
        
        return authors;
    }

    // 按等级查找作者
    std::vector<Author> getAuthorsByLevel(int level) {
        std::vector<Author> authors;
        
        std::string sql = "SELECT _id, _name, _brief_introduction, _level, "
                         "_is_transcription, _message_id "
                         "FROM authors WHERE _level = ?";
        
        std::unique_ptr<sql::PreparedStatement> pstmt(conn->prepareStatement(sql));
        pstmt->setInt(1, level);
        std::unique_ptr<sql::ResultSet> res(pstmt->executeQuery());
        
        while (res->next()) {
            Author author;
            author._id = res->getInt("_id");
            author._name = res->getString("_name");
            author._brief_introduction = res->getString("_brief_introduction");
            author._level = res->getInt("_level");
            author._is_transcription = res->getBoolean("_is_transcription");
            author._message_id = res->isNull("_message_id") ? 0 : res->getInt("_message_id");
            authors.push_back(author);
        }
        
        return authors;
    }
};

